#include "timer.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_gpio.h"
#include "stdio.h"
#include "usart.h"


/*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/
/*********************************************************************//**
 * @brief 		TIMER0 interrupt handler
 * @param		None
 * @return 		None
 ***********************************************************************/
 

 
void TIMER0_IRQHandler(void)
{
	uint8_t temp;
	
	TIM_Cmd(LPC_TIM0,DISABLE);
	TIM_ClearIntPending(LPC_TIM0, TIM_MR0_INT);

  UartAddBuffer();
	//TIM_ResetCounter(LPC_TIM0);
	//TIM_Cmd(LPC_TIM0,ENABLE);
}




void Timer0Init(uint32_t time)
{
	TIM_TIMERCFG_Type TIM_ConfigStruct;
	TIM_MATCHCFG_Type  TIM_MatchConfigStruct;

	TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL;
	// 1M 计数频率
	TIM_ConfigStruct.PrescaleValue  = 1;

	TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct);

	TIM_MatchConfigStruct.MatchChannel = 0;
	TIM_MatchConfigStruct.IntOnMatch   = TRUE;
	TIM_MatchConfigStruct.ResetOnMatch = TRUE;
	TIM_MatchConfigStruct.StopOnMatch = TRUE;//FALSE;
	TIM_MatchConfigStruct.ExtMatchOutputType = TIM_EXTMATCH_NOTHING;
	TIM_MatchConfigStruct.MatchValue   = time;

	TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct);

	TIM_ResetCounter(LPC_TIM0);
	NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01));
	NVIC_EnableIRQ(TIMER0_IRQn);
	//TIM_Cmd(LPC_TIM0,ENABLE);
}


void Time0Cmd(FunctionalState state)
{
  if(state==ENABLE){
    TIM_ResetCounter(LPC_TIM0);
    TIM_Cmd(LPC_TIM0,ENABLE);
  }
  else{
    TIM_Cmd(LPC_TIM0,DISABLE);
  }
}
